package com.community.web.user.controller.goods;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.common.login.LoginUserHolder;
import com.community.common.result.Result;
import com.community.model.entity.Goods;
import com.community.model.entity.GoodsCategory;
import com.community.model.entity.GoodsFavorites;
import com.community.web.user.service.GoodsFavoritesService;
import com.community.web.user.service.GoodsService;
import com.community.web.user.vo.GoodsDetailVo;
import com.community.web.user.vo.GoodsQueryVo;
import com.community.web.user.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "商品模块")
@RestController
@RequestMapping("/user/goods")
public class GoodsController {
    @Autowired
    private GoodsService service;

    @Autowired
    private GoodsFavoritesService favoritesService;


    @ApiOperation("根据条件分页查询商品列表")
    @GetMapping("{current}/{size}")
    public Result<IPage<GoodsVo>> pageGoodsList(@PathVariable long current, @PathVariable long size, GoodsQueryVo queryVo){
        Page<GoodsVo> page = new Page<>(current, size);
        IPage<GoodsVo> list = service.pageGoodsList(page, queryVo);
        return Result.ok(list);
    }

    @ApiOperation("根据商品id查询商品详情")
    @GetMapping("{id}")
    public Result<GoodsDetailVo> GoodsDetail(@PathVariable long id){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        GoodsDetailVo result = service.goodsDetail(id, userId);
        return Result.ok(result);
    }

    @ApiOperation("根据商品编号添加商品收藏")
    @PostMapping("{goodsNo}")
    public Result AddFavorite(@PathVariable String goodsNo){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        GoodsFavorites goodsFavorites = new GoodsFavorites();
        goodsFavorites.setGoodsNo(goodsNo);
        goodsFavorites.setUserId(userId.intValue());
        favoritesService.save(goodsFavorites);
        return Result.ok();
    }

    @ApiOperation("根据商品编号删除商品收藏")
    @DeleteMapping("{goodsNo}")
    public Result RemoveFavorite(@PathVariable String goodsNo){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        LambdaQueryWrapper<GoodsFavorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsFavorites::getGoodsNo, goodsNo);
        queryWrapper.eq(GoodsFavorites::getUserId, userId);
        favoritesService.remove(queryWrapper);
        return Result.ok();
    }

    @ApiOperation("根据商品编号查询商品详情")
    @GetMapping("no/{goodsNo}")
    public Result<GoodsDetailVo> GoodsDetailByGoodsNo(@PathVariable String goodsNo){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getGoodsNo, goodsNo);
        Goods goods = service.getOne(queryWrapper);
        GoodsDetailVo result = service.goodsDetail(goods.getId(), userId);
        return Result.ok(result);
    }
}
